<?php
/**
 * Created by PhpStorm.
 * User: 江雪
 * Date: 2019/6/15
 * Time: 16:21
 */

namespace app\common\controller;

use PHPMailer\PHPMailer\PHPMailer;
use think\Db;
use think\Exception;

class Email
{
    private static $type = [
        1 => '注册'
    ];
    public static function sendEmail($email,$type)
    {
        $res = self::sendCode($email, $type);
        return $res;
    }

    public  function sendCode($email, $type,$account = '', $password = '', $paypass = '')
    {
        try {
            $code = $this->randCode(6);
            //插入数据
            if (!$this->insertCode($email, $type, $code)) {
                return ['status' => 300, 'message' => '插入记录失败'];
            }
            //模板内容
            $content = $this->codeContent($code, $type, $account, $password, $paypass);
            $sms = $this->email($email, $content);
            if (!$sms) {
                return ['status' => 300, 'message' => '发送成功!'];
            }
            return ['status' => 200, 'message' => '发送成功!'];
        } catch (Exception $e) {
            return ['status' => 300, 'message' =>$e->getMessage()];
        }
    }
    public function insertCode($email,$type,$code){
        $data=[
            'email'=>$email,
            'event'=>self::$type[$type],
            'code'=>$code,
            'ip'=>$_SERVER['REMOTE_ADDR'] ?? "未查到",
            'createtime'=>time()
        ];
        $reslut= Db::name('ems')->insert($data);
        return $reslut;
    }
    /**
     * @param int $length
     * 随机码
     */
    public function randCode($length = 6)
    {

        $str = "";
        $ji = '0123456789';   #字符串可用下标的方式取值
        do {
            for ($i = 0; $i < $length; $i++) {
                $str .= $ji[mt_rand(0, strlen($ji) - 1)];
            }
        } while (strlen($str) != $length);
        return $str;
    }
    /**
     * 判断邮箱
     */
    public static function checkMail($param)
    {
        if (!filter_var($param, FILTER_VALIDATE_EMAIL)) {
            return false;#非法邮箱格式
        } else {
            return true;#正确邮箱格式
        }
    }
    //发送邮箱验证码
    public function email($email,$account = '', $password = '', $paypass = ''){
        $toemail =$email;//定义收件人的邮箱
        $mail = new PHPMailer();

        $mail->isSMTP();// 使用SMTP服务
        $mail->CharSet = "utf8";// 编码格式为utf8，不设置编码的话，中文会出现乱码
        $mail->Host = "smtp.gmail.com";// 发送方的SMTP服务器地址
        $mail->SMTPAuth = true;// 是否使用身份验证
        $mail->Username = "xiyue258012@gmail.com";// 发送方的QQ邮箱用户名，就是自己的邮箱名
        $mail->Password = "dajiang0101";// 发送方的邮箱密码，不是登录密码,是qq的第三方授权登录码,要自己去开启,在邮箱的设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 里面
        $mail->SMTPSecure = "ssl";// 使用ssl协议方式,
        $mail->Port = 465;// QQ邮箱的ssl协议方式端口号是465/587

        $mail->setFrom("xiyue258012@gmail.com","V8支付");// 设置发件人信息，如邮件格式说明中的发件人,
        $mail->addAddress($toemail,'用户');// 设置收件人信息，如邮件格式说明中的收件人
        $mail->addReplyTo("xiyue258012@gmail.com","V8支付");// 设置回复人信息，指的是收件人收到邮件后，如果要回复，回复邮件将发送到的邮箱地址
        //$mail->addCC("xxx@163.com");// 设置邮件抄送人，可以只写地址，上述的设置也可以只写地址(这个人也能收到邮件)
        //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
        //$mail->addAttachment("bug0.jpg");// 添加附件


        $mail->Subject = 'V8支付';// 邮件标题
        $mail->Body = $account;// 邮件正文
        //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容，如果不支持Html方式，就会用到这个，基本无用
        $mails = $mail->send();
        return $mails;
    }
    public function codeContent($code,$type,$account ='',$password ='',$paypass=''){
        switch ($type) {
            case 1:
                $content = '您下发订单的验证码是'.$code.'，请在5分钟内填写，为保障您的账户安全，切勿将验证码泄露给他人！【V8支付】';
                break;
            default:
                return false;
                break;
        }
        return $content;
    }
}